<?php
// No direct access to this file
defined('_JEXEC') or die;
 
// import the list field type
jimport('joomla.html.html');
jimport('joomla.form.formfield');
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');
 
/**
 * NGMenu Form Field class for the NGMenu component
 */
class JFormFieldBackgroundFiles extends JFormFieldList
{
	/**
	 * The field type.
	 *
	 * @var		string
	 */
	protected $type = 'BackgroundFiles';
 
//	public function getLabel() {
//	}
 
	public function getInput() {
	//build the list from all css files
		$bg_relpath = 'modules'.DS.'mod_ngmenu'.DS.'mod_ngmenu'.DS.'images'.DS.'background';
		$bg_path = JPATH_ROOT.DS.$bg_relpath;
		$bg_folder = @dir($bg_path);
		$bg_values = array();
		if ($bg_folder) {
			while ($bg_file = $bg_folder->read()){
				if ($bg_file != "." && $bg_file != ".." && $bg_file != "CVS" && $bg_file != "index.html" && is_file("$bg_path/$bg_file")){
					list($width, $height) = @getimagesize("$bg_path/$bg_file");
					$bg_relpath = str_replace( '\\', '/', $bg_relpath );
					$bg_values[] = JHTML::_('select.option', "/$bg_relpath/$bg_file", $bg_file.' (w:'.$width.'px h:'.$height.'px)');
//					$bg_values[] = JHTML::_('select.option',"JURI::Base()$bg_relpath/$bg_file", $bg_file.' (w:'.$width.'px h:'.$height.'px)');
				}
			}
			$bg_folder->close();
		}
		sort($bg_values);

		if ($this->name == 'jform[menu_back_image]'){
			$id = 'menu-back_image';
		}
		if ($this->name == 'jform[item_back_image_normal]'){
			$id = 'item-back_image_normal';
		}
		if ($this->name == 'jform[item_back_image_over]'){
			$id = 'item-back_image_over';
		}
		if ($this->name == 'jform[item_back_image_active]'){
			$id = 'item-back_image_active';
		}

		$prefix = JURI::root(true);
		$placeholder = JURI::root().'modules/mod_ngmenu/mod_ngmenu/images/blank20.gif';
		$options = 'onChange="preview_icon(this, \''.$id.'\', \''.$placeholder.'\', \''.$prefix.'\')"';
		
		return JHtml::_('select.genericlist', $bg_values, $this->name, $options, 'value', 'text', $this->value);
	}
}
